﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Silverlight.Testing;

namespace Twilight.Tests
{
    public static class Extensions
    {
        public static void WaitFor<T>(this SilverlightTest test, T objectToWaitForItsEvent, string eventName)
        {
            EventInfo eventInfo = objectToWaitForItsEvent.GetType().GetEvent(eventName);

            bool eventRaised = false;

            if (typeof(RoutedEventHandler).IsAssignableFrom(eventInfo.EventHandlerType))
                eventInfo.AddEventHandler(objectToWaitForItsEvent, (RoutedEventHandler)delegate { eventRaised = true; });
            else if (typeof(EventHandler).IsAssignableFrom(eventInfo.EventHandlerType))
                eventInfo.AddEventHandler(objectToWaitForItsEvent, (EventHandler)delegate { eventRaised = true; });

            test.EnqueueConditional(() => eventRaised);
        }
    }
}
